﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;

namespace getHTMLDoc
{
    class Program
    {
        static void Main(string[] args)
        {
            //Method3();
            Method4(null, null, "http://www.dmi.dk");
        }

        public static void Method1()
        {
            WebRequest request = WebRequest.Create("http://www.dmi.dk");
            WebResponse response = request.GetResponse();
            StreamReader r = new StreamReader(response.GetResponseStream());
            for (string line = r.ReadLine(); line != null; line = r.ReadLine())
            {
                Console.WriteLine(line);
            }

            Console.ReadLine();
        }

        public static void Method2()
        {
            WebClient client = new WebClient();
            Console.WriteLine(client.DownloadString("http://www.dmi.dk"));

            Console.ReadLine();
        }

        public static void Method3()
        {
            WebClient client = new WebClient();
            client.DownloadFile("http://www.dmi.dk", "dmi.html");
        }

        public static void Method4(string username, string password, string uri)
        {
            Stream stream;
            StreamReader reader;
            String response = null;
            WebClient webClient = new WebClient();

            // Check our auth details
            if (!string.IsNullOrEmpty(username) && !string.IsNullOrEmpty(password))
            {
                webClient.Credentials = new NetworkCredential(username, password);
            }

            try
            {
                // open and read from the supplied URI
                stream = webClient.OpenRead(uri);
                reader = new StreamReader(stream);
                response = reader.ReadToEnd();
            }
            catch (WebException ex)
            {
                Console.WriteLine(ex.Message);
            }

            Console.WriteLine(response);

            Console.ReadLine();
        }
    }
}
